package DataControl;

import Data.ClinetData;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * 客户端数据发送器
 */
public class ClientDataSender implements IClientDataSender{
    public Socket socket;
    private ClinetData clinetData;
    private String Password;
    public ObjectOutputStream outputStream = null;

    public ClientDataSender(Socket socket,String password)
    {
        this.socket=socket;
        clinetData=new ClinetData();
        clinetData.PassWord=password;
        Password=password;
    }
    public void setSocket(Socket socket){
        this.socket=socket;
    }
    /**
     * 设置密码
     * @param password
     */
    public void setPassword(String password)
    {
        Password=password;
    }

    /**
     * 设置命令
     * @param commend
     */
    public void setCommend(String commend) {
        clinetData=new ClinetData();
        if (commend == null || commend.trim().isEmpty()) {
            System.out.println("命令为空");
            return;
        }
        String[] commandParts = commend.split(" ");
        clinetData.Command=commandParts[0];
        if(commandParts.length>1)
        {
            clinetData.Param1=commandParts[1];
        }
    }

    /**
     * 发送命令
     * @param commend
     */
    public void Send(String commend) {
        assert socket != null : "客户端数据发送器：socket为空";
        try {
            setCommend(commend);
            outputStream = new ObjectOutputStream(socket.getOutputStream());
            outputStream.writeObject(clinetData);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ClinetData getClientData(){
        return this.clinetData;
    }
}
